package com.lvyou.ctrl.mgr;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.lvyou.entity.Goods;
import com.lvyou.model.JsonResult;
import com.lvyou.model.JsonResultPage;
import com.lvyou.service.GoodsSrv;
import com.lvyou.util.UUIDTools;

@RestController
@RequestMapping("/mgr/goods")
public class MgrGoodsCtrl {

	@Resource(name = "GoodsSrv")
	private GoodsSrv srv;

	@Value("${file.uploadFolder}")
	private String uploadFolder;

	@Value("${file.url}")
	private String fileUrl;
	
	@RequestMapping("/list")
	public JsonResult list(String name,String startDate,String endDate,Integer status,@RequestParam(value = "page",required = false) String pageNum,@RequestParam(value = "limit",required = false)String pageSize){
		JsonResultPage jr=new JsonResultPage();
		if(startDate!=null && startDate.length()==10) {
			startDate+=" 00:00:00";
		}
		if(endDate!=null && endDate.length()==10) {
			endDate+=" 23:59:59";
		}
		int pageNumInt=0;
		int pageSizeInt=10;
		
		if(pageNum!=null && !"".equals(pageNum)) {
			pageNumInt=Integer.parseInt(pageNum);
		}
		if(pageSize!=null && !"".equals(pageSize)) {
			pageSizeInt=Integer.parseInt(pageSize);
		}
		
		try {
			PageInfo<Goods> page=srv.mgrQueryList(name, startDate, endDate,status, pageNumInt,pageSizeInt);
			jr.setCode(0);
			jr.setData(page.getList());
			jr.setTotal(page.getTotal());
			jr.setPageNum(pageNumInt);
			jr.setPageSize(pageSizeInt);
			jr.setPages(page.getPages());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}
		
		return jr;
	}
	@PostMapping("/add")
	public JsonResult add(Goods goods){
		JsonResult jr=new JsonResult();;
		try {
			srv.add(goods);
			jr.setCode(0);
			jr.setMsg("添加成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}
		
		return jr;
	}
	
	@PostMapping("/deleteByIds")
	public JsonResult deleteByIds(@RequestParam(name = "ids[]") String[] ids){
		JsonResult jr=new JsonResult();
		try {
			srv.deleteByIds(ids);
			jr.setCode(0);
			jr.setMsg("删除成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误！");
		}
		
		return jr;
	}
	
	@PostMapping("/updateById")
	public JsonResult updateById(Goods goods){
		JsonResult jr=new JsonResult();;
		try {
			int i=srv.updateById(goods);
			System.out.println(i);
			jr.setCode(0);
			if(i>0) {
				jr.setMsg("更新成功！");
			}else {
				jr.setMsg("更新失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}
		
		return jr;
	}
	@RequestMapping("/showGoodsById")
	public JsonResult showGoodsById(String id){
		JsonResult jr=new JsonResult();
		try {
			Goods goods=srv.showGoodsById(id);
			jr.setCode(0);
			jr.setData(goods);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jr.setCode(100000);
			jr.setMsg("发生错误");
		}
		
		return jr;
	}
	@PostMapping("/upload")
	public JsonResult upload(MultipartFile image,HttpServletRequest req) {
		JsonResult<Map<String, String>> jr=new JsonResult();

		String oFilename=image.getOriginalFilename();
		String suffix=oFilename.substring(oFilename.lastIndexOf("."));
		String newFilename=UUIDTools.randomUUID()+suffix;
		
		//前台页面回显图片的url
		String imageURL=fileUrl+newFilename;

		File file=new File(uploadFolder);
		if(!file.exists()) {
			file.mkdirs();
		}
		File filePath=new File(file,newFilename);

		try {
			image.transferTo(filePath);
			jr.setCode(0);
			
			Map<String, String> map=new HashMap<String, String>();
			map.put("imageURL", imageURL);//页面回显图片
			map.put("dbURL", imageURL);//保存到数据库的url
			jr.setData(map);
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jr;
	}
}
